<?php

namespace App\Http\Controllers\Web;

use App\Http\Requests\UserRequest;
use App\Services\User;
use Illuminate\Http\Request;

class UserController extends BaseController
{
    /**
     * UserController constructor.
     */
    public function __construct()
    {
        $this->service = User::getInstance();
    }

    /**
     * index 用户列表
     * @param UserRequest $request
     * @return object
     * @throws \Exception
     */
    public function index(UserRequest $request)
    {
        // 关键词匹配
        if (!empty($request->get('keyword'))) {
            $condition[] = ['users.username', 'like', '%' . $request->get('keyword') . '%'];
        }
        // 状态值
        if (!empty($request->get('status'))) {
            $condition[] = ['users.status', '=', $request->get('status')];
        }

        $list = $this->service->getList($condition ?? []);

        return $this->success('查询成功', $list);
    }

    /**
     * store 新增用户
     * @param UserRequest $request
     * @return object
     * @throws \Exception
     */
    public function store(UserRequest $request)
    {
        $this->service->insertData($request->all());

        return $this->success('新增成功', []);
    }

    /**
     * update 更新用户
     * @param UserRequest $request
     * @param int $id
     * @return object
     * @throws \Exception
     */
    public function update(UserRequest $request, int $id)
    {
        $this->service->updateData($id, $request->all());

        return $this->success('更新成功', []);
    }

    public function login(UserRequest $request)
    {
        $result = $this->service->login($request->all());
        if ($result) {
            $list['token'] = 'Bearer ' . $result;
            // $menus = $this->service->getUserMenus();
            // $list['menu'] = $menus['menu'] ?? [];
            // $list['url'] = $menus['url'] ?? [];

            return $this->success('登录成功', $list);
        } else {
            return $this->error('登录失败');
        }
    }

    /**
     * logout 注销登录
     * @param Request $request
     * @return object
     * @throws \Exception
     */
    public function logout(Request $request)
    {
        $this->service->logout();

        return $this->success('注销成功');
    }

    /**
     * menu 获取用户拥有的菜单
     * @param Request $request
     * @return object
     */
    // public function menu(Request $request)
    // {
    //     $result = $this->service->getUserMenus();
    //
    //     return $this->success('获取成功', $result);
    // }

    public function menu()
    {
        return $this->success('获取成功', []);
    }

    public function info()
    {
        return $this->success('获取成功', [
            'roles' => 'seo',
            'name' => '小混混',
            'avatar' => 'http://www.baidu.com',
            'menu1' => [
                [
                    'id' => 1,
                    'pid' => 0,
                    'name' => '权限管理111',
                    'path' => '#', // 设置#代表
                    // 'url' => '/auth',
                    'meta' => [
                        'icon' => 'el-icon-s-help',
                        'title' => '权限管理2222',
                        // 'breadcrumb' => false
                    ],
                    'redirect' => '#',
                    'hidden' => false,
                    'component' => '#',
                    'outer_link' => 0,
                    'alwaysShow' => true,
                    'children' => [
                        [
                            'id' => 33,
                            'pid' => 30,
                            'name' => '用户管理',
                            'path' => '/auth/user',
                            // 'url' => '/auth/user',
                            'meta' => [
                                'icon' => '',
                                'title' => '用户管理',
                                'activeMenu' => '/auth/user/list',
                            ],
                            "hidden" => false,
                            'component' => '/auth/index',
                            'redirect' => '/auth/user/list',
                            'alwaysShow' => true,
                            'outer_link' => 0,
                            'children' => [
                                [
                                    'id' => 33,
                                    'pid' => 30,
                                    'name' => '用户列表',
                                    'path' => '/auth/user/list',
                                    // 'url' => '/auth/user/list',
                                    'meta' => [
                                        'icon' => '',
                                        'title' => '用户列表',
                                        'activeMenu' => '/auth/user',
                                    ],
                                    "hidden" => true,
                                    'component' => '/auth/user/list',
                                    'alwaysShow' => true,
                                    'outer_link' => 0,
                                    'children' => [

                                    ],
                                ],
                                [
                                    'id' => 33,
                                    'pid' => 30,
                                    'name' => '新增用户',
                                    'path' => '/auth/user/add',
                                    // 'url' => '/auth/user/add',
                                    'meta' => [
                                        'icon' => '',
                                        'title' => '新增用户',
                                        'activeMenu' => '/auth/user',
                                    ],
                                    "hidden" => true,
                                    'component' => '/auth/user/add',
                                    'alwaysShow' => true,
                                    'outer_link' => 0,
                                    'children' => [

                                    ],
                                ],
                                [
                                    'id' => 33,
                                    'pid' => 30,
                                    'name' => '编辑用户',
                                    'path' => '/auth/user/edit/:id(\d+)',
                                    // 'url' => '/auth/user/add',
                                    'meta' => [
                                        'icon' => '',
                                        'title' => '编辑用户',
                                        'activeMenu' => '/auth/user',
                                    ],
                                    "hidden" => true,
                                    'component' => '/auth/user/edit',
                                    'alwaysShow' => true,
                                    'outer_link' => 0,
                                    'children' => [

                                    ],
                                ],
                            ],
                        ],
                        [
                            'id' => 33,
                            'pid' => 30,
                            'name' => '角色管理',
                            'path' => '/auth/role',
                            // 'url' => '/auth/role',
                            'meta' => [
                                'icon' => '',
                                'title' => '角色管理',
                                'activeMenu' => '/auth/role',
                            ],
                            "hidden" => false,
                            'component' => '/auth/index',
                            'redirect' => '/auth/role/list',
                            'alwaysShow' => true,
                            'outer_link' => 0,
                            'url' => 'http://www.baidu.com',
                            'children' => [
                                [
                                    'id' => 33,
                                    'pid' => 30,
                                    'name' => '角色列表',
                                    'path' => '/auth/role/list',
                                    // 'url' => '/auth/role/list',
                                    'meta' => [
                                        'icon' => '',
                                        'title' => '角色列表',
                                        'activeMenu' => '/auth/role',
                                    ],
                                    "hidden" => true,
                                    'component' => '/auth/role/list',
                                    'alwaysShow' => true,
                                    'children' => [

                                    ],
                                ],
                                [
                                    'id' => 33,
                                    'pid' => 30,
                                    'name' => '新增角色',
                                    'path' => '/auth/role/add',
                                    // 'url' => '/auth/role/list',
                                    'meta' => [
                                        'icon' => '',
                                        'title' => '新增角色',
                                        'activeMenu' => '/auth/role',
                                    ],
                                    "hidden" => true,
                                    'component' => '/auth/role/add',
                                    'alwaysShow' => true,
                                    'children' => [

                                    ],
                                ],
                                [
                                    'id' => 33,
                                    'pid' => 30,
                                    'name' => 'RoleEdit',
                                    'path' => '/auth/role/edit/:id(\d+)',
                                    // 'url' => '/auth/role/list',
                                    'meta' => [
                                        'icon' => '',
                                        'title' => '编辑角色',
                                        'activeMenu' => '/auth/role',
                                    ],
                                    "hidden" => true,
                                    'component' => '/auth/role/edit',
                                    'alwaysShow' => true,
                                    'children' => [

                                    ],
                                ],
                            ],
                        ],
                        [
                            'id' => 33,
                            'pid' => 30,
                            'name' => '菜单管理',
                            'path' => '/auth/menu',
                            // 'url' => '/auth/role',
                            'meta' => [
                                'icon' => '',
                                'title' => '菜单管理',
                                'activeMenu' => '/auth/menu',
                            ],
                            "hidden" => false,
                            'component' => '/auth/index',
                            'redirect' => '/auth/menu/list',
                            'alwaysShow' => true,
                            'outer_link' => 0,
                            'url' => 'http://www.baidu.com',
                            'children' => [
                                [
                                    'id' => 33,
                                    'pid' => 30,
                                    'name' => '菜单列表',
                                    'path' => '/auth/menu/list',
                                    // 'url' => '/auth/role/list',
                                    'meta' => [
                                        'icon' => '',
                                        'title' => '菜单列表',
                                        'activeMenu' => '/auth/menu',
                                    ],
                                    "hidden" => true,
                                    'component' => '/auth/menu/list',
                                    'alwaysShow' => true,
                                    'children' => [

                                    ],
                                ],
                                [
                                    'id' => 33,
                                    'pid' => 30,
                                    'name' => 'MenuAdd',
                                    'path' => '/auth/menu/add',
                                    // 'url' => '/auth/role/list',
                                    'meta' => [
                                        'icon' => '',
                                        'title' => '新增菜单',
                                        'activeMenu' => '/auth/menu',
                                    ],
                                    "hidden" => true,
                                    'component' => '/auth/menu/add',
                                    'alwaysShow' => true,
                                    'children' => [

                                    ],
                                ],
                                [
                                    'id' => 33,
                                    'pid' => 30,
                                    'name' => 'MenuEdit',
                                    'path' => '/auth/menu/edit/:id(\d+)',
                                    // 'url' => '/auth/user/add',
                                    'meta' => [
                                        'icon' => '',
                                        'title' => '编辑菜单',
                                        'activeMenu' => '/auth/menu',
                                    ],
                                    "hidden" => true,
                                    'component' => '/auth/menu/edit',
                                    'alwaysShow' => true,
                                    'outer_link' => 0,
                                    'children' => [

                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
                // [
                //     'id' => 1,
                //     'pid' => 0,
                //     'name' => '外链管理',
                //     'path' => '#', // 设置#代表
                //     // 'url' => '/auth',
                //     'meta' => [
                //         'icon' => 'el-icon-s-help',
                //         'title' => '权限管理',
                //         // 'breadcrumb' => false
                //     ],
                //     'redirect' => '#',
                //     'hidden' => false,
                //     'component' => '#',
                //     'outer_link' => 1,
                //     'url' => 'http://www.baidu.com',
                //     'children' => [

                //     ],
                // ],
            ],
            'menu' => $this->service->getUserMenus()
        ]);
    }
}
